﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProjectTest
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        private const int DRAG_HANDLE_SIZE = 7;

        private Image[] component_image;

        private double canvasLeft;
        private double canvasTop;

        private double deltaTop;
        private double deltaLeft;

        private int imagecount = 0;

        private Component cmp_temp;

        private Component test;
        private WPFBrushList _brushes;

        private List<Image> clickImage;

        public MainWindow()
        {
            InitializeComponent();

            _brushes = new WPFBrushList();
            lsbBrushes.DataContext = _brushes;

            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(@"c:\users\jeongkw\documents\visual studio 2010\Projects\SVN\ProjectTest\ProjectTest\test\a.jpg");
            bitmap.EndInit();
            img.BeginInit();
            img.Source = bitmap;

            img.PreviewMouseDown += new MouseButtonEventHandler(img_PreviewMouseDown);
            img.PreviewMouseMove += new MouseEventHandler(img_PreviewMouseMove);
            img.PreviewMouseUp += new MouseButtonEventHandler(img_PreviewMouseUp);
            img.LostMouseCapture += new MouseEventHandler(img_LostMouseCapture);
            img.SizeChanged += new SizeChangedEventHandler(img_SizeChanged);
            img.EndInit();

            component_image = new Image[50];



            bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(@"c:\users\jeongkw\documents\visual studio 2010\Projects\SVN\ProjectTest\ProjectTest\test\activity_icon.jpg");
            bitmap.EndInit();
            component_image[0] = new Image();
            component_image[0].BeginInit();
            component_image[0].Source = bitmap;
            component_image[0].Width = 100;
            component_image[0].Height = 30;
            component_image[0].EndInit();


            bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(@"c:\users\jeongkw\documents\visual studio 2010\Projects\SVN\ProjectTest\ProjectTest\test\tablet_icon.jpg");
            bitmap.EndInit();
            component_image[1] = new Image();
            component_image[1].BeginInit();
            component_image[1].Source = bitmap;
            component_image[1].Width = 100;
            component_image[1].Height = 30;
            component_image[1].EndInit();


            bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(@"C:\Users\JeongKW\documents\visual studio 2010\Projects\SVN\ProjectTest\ProjectTest\test\b.jpg");
            bitmap.EndInit();
            test = new Component();
            test.BeginInit();
            test.Source = bitmap;
            test.Width = 674;
            test.Height = 515;
            test.Stretch = Stretch.Fill;
            //test.PreviewMouseDown += new MouseButtonEventHandler();
            test.EndInit();
            int zindex = canvas.Children.Count;
            canvas.Children.Add(test);
            Canvas.SetZIndex(test, zindex+12);
            Canvas.SetLeft(test, 50);
            Canvas.SetTop(test, 50);

            setting_Resize(test);

            test.position = new Point(10, 10);
            test.PreviewMouseDown += new MouseButtonEventHandler(cmt_PreviewMouseDown);
            test.PreviewMouseMove += new MouseEventHandler(cmt_PreviewMouseMove);
            test.PreviewMouseUp += new MouseButtonEventHandler(cmt_PreviewMouseUp);
            test.LostMouseCapture += new MouseEventHandler(cmt_LostMouseCapture);

            clickImage = new List<Image>();
        }

        void setting_Resize(Component _cmp)
        {
            _cmp.re = new Resize(_cmp, new Point(Canvas.GetLeft(_cmp), Canvas.GetTop(_cmp)));
            int zindex = canvas.Children.Count;
            for (int i = 0; i < 4; i++)
            {
                Line li = _cmp.re.get_Line(i);
                canvas.Children.Add(li);
                li.Visibility = Visibility.Visible;
                Canvas.SetZIndex(li, zindex + i);
                Canvas.SetLeft(li, _cmp.re.get_RectSize(_cmp.re.LINE_ARRY[i]).X);
                Canvas.SetTop(li, _cmp.re.get_RectSize(_cmp.re.LINE_ARRY[i]).Y);
            }
            for (int i = 0; i < 8; i++)
            {
                Image rt = _cmp.re.get_Rectangle(i);
                canvas.Children.Add(rt);
                rt.Visibility = Visibility.Visible;
                Canvas.SetZIndex(rt, zindex + i + 6);
                Canvas.SetLeft(rt, _cmp.re.get_RectSize(i).X);
                Canvas.SetTop(rt, _cmp.re.get_RectSize(i).Y);
            }
            Image _rt = _cmp.re.get_Rectangle(8);
            canvas.Children.Add(_rt);
            _rt.Visibility = Visibility.Hidden;
            Canvas.SetZIndex(_rt, zindex + 1);
            Canvas.SetLeft(_rt, _cmp.re.get_RectSize(8).X);
            Canvas.SetTop(_rt, _cmp.re.get_RectSize(8).Y);
            _cmp.re.visiblie(Visibility.Hidden);
            
        }

        void img_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //throw new NotImplementedException();
        }




        /*
         * 
         * 이미지 이동 이벤트
         * 
         */


        private void set_ImageMove(Point posion, Image _img)
        {
            this.imagecount++;
        }


        private void img_LostMouseCapture(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = null;
            ((Image)sender).ReleaseMouseCapture();
            output.Text = "mouse lost";
        }


        private void img_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            Mouse.OverrideCursor = null;
            ((Image)sender).ReleaseMouseCapture();
            output.Text = e.GetPosition(this.canvas).ToString();
        }

        private void img_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (((Image)sender).IsMouseCaptured)
            {
                Point mouseCurrent = e.GetPosition(null);
                double Left = mouseCurrent.X - canvasLeft;
                double Top = mouseCurrent.Y - canvasTop;
                ((Image)sender).SetValue(Canvas.LeftProperty, canvasLeft + Left - deltaLeft);
                ((Image)sender).SetValue(Canvas.TopProperty, canvasTop + Top - deltaTop);
                canvasLeft = Canvas.GetLeft(((Image)sender));
                canvasTop = Canvas.GetTop(((Image)sender));
                output.Text = e.GetPosition(this.canvas).ToString();
            }
        }

        private void img_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

            Mouse.OverrideCursor = Cursors.Hand;
            canvasLeft = Canvas.GetLeft(((Image)sender));
            canvasTop = Canvas.GetTop(((Image)sender));
            Point pnt = e.GetPosition((Image)sender);
            deltaLeft = pnt.X;
            deltaTop = pnt.Y;
            ((Image)sender).CaptureMouse();
            Image temp = (Image)sender;
            
        }

        private void cmt_LostMouseCapture(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = null;
            ((Component)sender).ReleaseMouseCapture();
            //((Component)sender).re.visiblie(Visibility.Hidden);
            output.Text = "mouse lost";
        }


        private void cmt_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            Mouse.OverrideCursor = null;
            ((Component)sender).ReleaseMouseCapture();
            output.Text = e.GetPosition(this.canvas).ToString();
        }

        private void cmt_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (((Component)sender).IsMouseCaptured)
            {
                Point mouseCurrent = e.GetPosition(null);
                double Left = mouseCurrent.X - canvasLeft;
                double Top = mouseCurrent.Y - canvasTop;
                ((Component)sender).SetValue(Canvas.LeftProperty, canvasLeft + Left - deltaLeft);
                ((Component)sender).SetValue(Canvas.TopProperty, canvasTop + Top - deltaTop);
                ((Component)sender).re.SetPosition(canvasLeft + Left - deltaLeft, canvasTop + Top - deltaTop, 0);
                canvasLeft = Canvas.GetLeft(((Image)sender));
                canvasTop = Canvas.GetTop(((Image)sender));
                ((Component)sender).re.transformation(sender);
                output.Text = e.GetPosition(this.canvas).ToString();
            }
        }

        private void cmt_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (cmp_temp != null)
                cmp_temp.re.visiblie(Visibility.Hidden);
            Mouse.OverrideCursor = Cursors.Hand;
            canvasLeft = Canvas.GetLeft(((Image)sender));
            canvasTop = Canvas.GetTop(((Image)sender));
            Point pnt = e.GetPosition((Image)sender);
            deltaLeft = pnt.X;
            deltaTop = pnt.Y;
            ((Component)sender).CaptureMouse();
            Component temp = (Component)sender;
            output.Text = e.GetPosition(this.canvas).ToString();
            temp.re.visiblie(Visibility.Visible);
            cmp_temp = temp;
        }
        
        private void Canvas_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {

        }

        private void canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void CopyName_Click(object sender, RoutedEventArgs e)
        {
            if (lsbBrushes.SelectedIndex != -1)
                Clipboard.SetText(((WPFBrush)lsbBrushes.SelectedItem).Name);
        }

        private void CopyHex_Click(object sender, RoutedEventArgs e)
        {
            if (lsbBrushes.SelectedIndex != -1)
                Clipboard.SetText(((WPFBrush)lsbBrushes.SelectedItem).Hex);
        }


    }
}
